const axios = require('axios');

const API_URL = 'http://localhost:3000/api';

// 测试API的函数
async function testApi() {
  console.log('开始测试后端API...');
  
  try {
    // 测试1: 获取北京的天气
    console.log('\n测试1: 获取北京的天气');
    const weatherResponse = await axios.get(`${API_URL}/weather/北京/北京`);
    
    if (weatherResponse.data.status === 'success') {
      console.log('✅ 成功获取北京天气数据');
      
      // 检查体感温度字段
      const weather = weatherResponse.data.data.weather;
      if (weather.temperatureFeelsLike !== undefined) {
        console.log('✅ 体感温度字段存在:', weather.temperatureFeelsLike);
      } else {
        console.log('❌ 体感温度字段不存在');
      }
      
      // 检查天气预报字段
      if (weather.forecasts && weather.forecasts.length > 0) {
        const forecast = weather.forecasts[0];
        console.log('✅ 天气预报数据存在，共', weather.forecasts.length, '条');
        
        // 检查新增的预报字段
        const newFields = [
          'sunrise', 'sunset', 'weatherConditionNight', 'textDay', 'textNight',
          'windScale', 'windSpeedNight', 'windDirectionNight', 'windScaleNight',
          'humidity', 'precipitation', 'pressure', 'visibility', 'uvIndex'
        ];
        
        let existingFields = [];
        let missingFields = [];
        
        newFields.forEach(field => {
          if (forecast[field] !== undefined) {
            existingFields.push(field);
          } else {
            missingFields.push(field);
          }
        });
        
        if (existingFields.length > 0) {
          console.log(`✅ 存在的新增字段: ${existingFields.join(', ')}`);
        }
        
        if (missingFields.length > 0) {
          console.log(`❌ 缺失的字段: ${missingFields.join(', ')}`);
        }
      } else {
        console.log('❌ 天气预报数据不存在');
      }
    } else {
      console.log('❌ 获取北京天气数据失败:', weatherResponse.data);
    }
    
    // 测试2: 获取热门城市
    console.log('\n测试2: 获取热门城市');
    const popularResponse = await axios.get(`${API_URL}/weather/popular`);
    
    if (popularResponse.data && popularResponse.data.code === "200") {
      console.log('✅ 成功获取热门城市数据');
    } else {
      console.log('❌ 获取热门城市数据失败:', popularResponse.data);
    }
    
    console.log('\n测试完成! 👍');
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    if (error.response) {
      console.error('服务器响应:', error.response.data);
    }
  }
}

// 运行测试
testApi(); 